package com.bnlr.lrparking.appui.bank.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.model.StagesBankBean;
import com.yolanda.nohttp.rest.Request;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/4 0004.
 */

public class StagesBankAdapter extends BaseAdapter{
    public Request<String> mRequest;
    private Context mcontext;
    private List<StagesBankBean.DataBean> list=new ArrayList<>();
    private LayoutInflater inflater;
    private int defaultSelection =  0;


    public StagesBankAdapter(Context context, List<StagesBankBean.DataBean> list) {
        super();
        this.mcontext = context;
        this.list = list;
        inflater = LayoutInflater.from(mcontext);
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        final StagesBankAdapter.ViewHoder hoder;
        if (convertView == null) {
            hoder = new StagesBankAdapter.ViewHoder();
            convertView = inflater.inflate(R.layout.item_bankcard, null);
            hoder.item_name = (TextView) convertView.findViewById(R.id.item_name);
            hoder.item_card=(TextView) convertView.findViewById(R.id.item_card);
            hoder.img_select=(ImageView) convertView.findViewById(R.id.img_select);
            hoder.select_bank_card=(ImageView)convertView.findViewById(R.id.select_bank_card);
            convertView.setTag(hoder);
        } else {
            hoder = (StagesBankAdapter.ViewHoder) convertView.getTag();
        }
        final StagesBankBean.DataBean bean = list.get(i);
        hoder.item_name.setText(bean.getBankName());
        Glide.with(mcontext).load(bean.getBankIconUrl()).into(hoder.select_bank_card);
        hoder.item_card.setVisibility(View.GONE);
        if (i == defaultSelection) {// 选中时设置单纯颜色
            hoder.img_select.setVisibility(View.VISIBLE);
        } else {// 未选中时设置selector
            hoder.img_select.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHoder {
        TextView item_name,item_card;
        ImageView img_select,select_bank_card;
    }

    public void setSelectPosition(int i) {
        if (!(i < 0 || i > list.size())) {
            defaultSelection = i;
            notifyDataSetChanged();
        }
    }
}
